<?
class EditPageAdminForm extends Form{
	private $page=array();
	function EditPageAdminForm(){
		Form::Form('EditPageAdminForm');
		if(Url::get('cmd')=='copy')
			CGlobal::$website_title="Copy Page";
		else
			CGlobal::$website_title="Sửa Page";
		if(Url::get('cmd')=='edit' || Url::get('cmd')=='copy'){			
			$id = (int)Url::get('id');
			if($id){
				$this->page=DB::select('page','id='.$id);
			}
			
			if(!$this->page)
			Url::redirect_current();
		}
		else{
			$this->page=array(
								'name'		  =>'',
								'title'	  	  =>'',
								'layout'	  =>'',
								'description' =>''
							);
		}
	}
	
	function on_submit(){
		$name			=trim(Url::get('name'));
		$title			=trim(Url::get('title'));
		$layout			=trim(Url::get('layout'));
		$description 	=trim(Url::get('description'));
		
		$this->checkFormInput('Tên page','name',$name,'uname',true,'',1,255);
		
		if(!$layout){
			$this->setFormError('layout','Bạn chưa chọn Layout!');
		}
		elseif(!file_exists(ROOT_PATH.$layout)){
			$this->setFormError('layout','Layout không tồn tại!');
		}
		
		$this->checkFormInput('Tiêu đề','title',$title,'str',false,'',0,255);
		$this->checkFormInput('Mô tả','description',$description,'str',false,'',0,255);
		
		if(!$this->errNum){
			$new_row 				= array(
											'name'		  =>$name,
											'title'		  =>$title,
											'layout'	  =>$layout,
											'description' =>$description
										);

										
			if(Url::get('cmd')=='copy'){
				if($name==$this->page['name'] || DB::select('page','name="'.$name.'"')){
					$this->setFormError('name','Tên trang "'.$name.'" đã tồn tại, hãy chọn tên khác!');
					return ;
				}
				
				
				$id = DB::insert('page', $new_row);
				
				if($id){
					$re=DB::query('SELECT id, module_id, page_id, region, position FROM block WHERE page_id='.$this->page['id']);
					if($re){
						while ($row=mysql_fetch_assoc($re)){
							unset($row['id']);
							$row['page_id']=$id;
							DB::insert('block',$row);
						}
					}
				}
			}									
			elseif(Url::get('cmd')=='edit'){
				DB::update('page', $new_row,'id='.$this->page['id']);
				ECore::update_page($this->page['id']);
			}
			else{
				$id = DB::insert('page', $new_row);
			}
			
			Url::redirect_current();
		}
	}	
	
	function draw(){	
		global $display;
		$this->beginForm();
		
		if(Url::get('cmd')=='edit')
			$display->add('mode',"Sửa");
		elseif(Url::get('cmd')=='copy')
			$display->add('mode',"Copy");
		else
			$display->add('mode',"Thêm");
			
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->add('name',Url::get('name',$this->page['name']));
		$display->add('title',Url::get('title',$this->page['title']));
		$display->add('description',Url::get('description',$this->page['description']));
		$display->add('option_layout',HTML::getOption($this->get_all_layouts(),Url::get('status',$this->page['layout'])));
		$display->output('edit');
		$this->endForm();
	}
	
	function get_all_layouts(){
		//Code mới: TuấnNK ( 20080606 15h36
		$layouts = array(''=>'-- Chọn layout --');
		$dir = opendir(ROOT_PATH.'layouts');
		while($file = readdir($dir)){
			if($file != '.' and $file != '..' and is_file('layouts/'.$file)){
				$layouts['layouts/'.$file] = basename($file,Upload::getExtension($file));
			}
		}
		closedir($dir);
		return $layouts;
	}
}
?>